package com.lagou.utils;

import com.lagou.annotation.MyAutowire;
import com.lagou.annotation.MyComponent;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

@MyComponent("connectionUtil")
public class ConnectionUtil {

    private Connection connection;

    @MyAutowire("druidUtil")
    private DruidUtil druidUtil;

    public synchronized Connection getConnection(){
        if (connection == null){
            try {
                connection = druidUtil.getDruidDataSource().getConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return connection;
    }
}
